package com.androider.timememory.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.androider.timememory.R;
import com.androider.timememory.activity.AlbumPlayAudio;
import com.androider.timememory.activity.AlbumPlayPhoto;
import com.androider.timememory.activity.AlbumPlayVideo;
import com.androider.timememory.activity.Note;
import com.androider.timememory.activity.Photo;
import com.androider.timememory.activity.Video;
import com.androider.timememory.db.Record;
import com.androider.timememory.db.RecordModel;
import com.androider.timememory.utils.ImageUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RecordAdapter extends BaseAdapter {
	
	private static final int TYPE_ITEM_TEXT = 0;
	private static final int TYPE_ITEM_PHOTO = 1;
	private static final int TYPE_ITEM_VIDEO = 2;
	private static final int TYPE_ITEM_AUDIO = 3;
	private static final int TYPE_MAX_COUNT = TYPE_ITEM_AUDIO + 1;
	
    private class ViewHolder {
        private TextView date;
        private ImageView category;
        private TextView type;
        private TextView note;
        private ImageView image;
    }
    
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<HashMap<String, Object>> mData;
	private final String typeText; 
	private final String typePhoto; 
	private final String typeVideo; 
	private final String typeAudio; 
	
	public RecordAdapter(Context context, ArrayList<HashMap<String, Object>> data) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mData = data;
		final Resources res = mContext.getResources();
		typeText = res.getString(R.string.list_type_text);
		typePhoto = res.getString(R.string.list_type_photo);
		typeVideo = res.getString(R.string.list_type_video);
		typeAudio = res.getString(R.string.list_type_audio);
	}
	
	@Override
	public int getItemViewType(int position) {
		final String type = (String) mData.get(position).get("type");
		if(type.equals(typeText)) {
			return TYPE_ITEM_TEXT;
		} else if(type.equals(typePhoto)) {
			return TYPE_ITEM_PHOTO;
		} else if(type.equals(typeVideo)) {
			return TYPE_ITEM_VIDEO;
		} else if(type.equals(typeAudio)) {
			return TYPE_ITEM_AUDIO;
		} else {
			return -1;
		}
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		String type = (String) mData.get(position).get("type");
		final int itemType = getItemViewType(position);
		if(null == convertView) {
			holder = new ViewHolder();
			switch(itemType) {
			case TYPE_ITEM_TEXT:
				convertView = mInflater.inflate(R.layout.list_item_text, null);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.category = (ImageView) convertView.findViewById(R.id.category);
				holder.type = (TextView) convertView.findViewById(R.id.type);
				holder.note = (TextView) convertView.findViewById(R.id.note);
				break;
			case TYPE_ITEM_PHOTO:
				convertView = mInflater.inflate(R.layout.list_item_photo, null);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.category = (ImageView) convertView.findViewById(R.id.category);
				holder.type = (TextView) convertView.findViewById(R.id.type);
				holder.note = (TextView) convertView.findViewById(R.id.note);
				holder.image = (ImageView) convertView.findViewById(R.id.image);
				break;
			case TYPE_ITEM_VIDEO:
				convertView = mInflater.inflate(R.layout.list_item_video, null);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.category = (ImageView) convertView.findViewById(R.id.category);
				holder.type = (TextView) convertView.findViewById(R.id.type);
				holder.note = (TextView) convertView.findViewById(R.id.note);
				holder.image = (ImageView) convertView.findViewById(R.id.image);
				break;
			case TYPE_ITEM_AUDIO:
				convertView = mInflater.inflate(R.layout.list_item_audio, null);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.category = (ImageView) convertView.findViewById(R.id.category);
				holder.type = (TextView) convertView.findViewById(R.id.type);
				holder.note = (TextView) convertView.findViewById(R.id.note);
				holder.image = (ImageView) convertView.findViewById(R.id.image);
				break;
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
//		convertView.setTag(R.id.tag_id, mData.get(position).get("id"));
		holder.date.setText((String) mData.get(position).get("date"));
		holder.category.setImageResource((Integer) mData.get(position).get("category"));
		holder.type.setText(type);
		holder.note.setText((String) mData.get(position).get("note"));
		
		if(holder.image != null && (itemType != (TYPE_ITEM_TEXT | TYPE_ITEM_AUDIO))) {
			holder.image.setImageBitmap(ImageUtil.getSmallThumbnail(mContext, (String) mData.get(position).get("content"), type));
		}
		
		if(holder.image != null) {
			holder.image.setTag(R.id.tag_id, mData.get(position).get("id"));
			
			holder.image.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					switch(itemType) {
						case TYPE_ITEM_PHOTO:
							intent.setClass(mContext, AlbumPlayPhoto.class);
							intent.putExtra(AlbumPlayPhoto.PHOTO_PATH, (String) mData.get(position).get("content"));
							mContext.startActivity(intent);
							((Activity) mContext).overridePendingTransition(
							 R.anim.activity_zoomin, R.anim.activity_zoomout);
							break;
						case TYPE_ITEM_VIDEO:
							intent.setClass(mContext, AlbumPlayVideo.class);
							intent.putExtra(AlbumPlayVideo.VIDEO_PATH, (String) mData.get(position).get("content"));
							mContext.startActivity(intent);
							break;
						case TYPE_ITEM_AUDIO:
							intent.setClass(mContext, AlbumPlayAudio.class);
							intent.putExtra(AlbumPlayAudio.AUDIO_PATH, (String) mData.get(position).get("content"));
							intent.putExtra(AlbumPlayAudio.AUDIO_TITLE, (String) mData.get(position).get("note"));
							mContext.startActivity(intent);
							break;
						default:
							break;
					}
				}
				
			});
		}
		
		
//		new ImageAsyncTask().execute((String) mData.get(position).get("content"), type, holder.image);
		return convertView;
	}
	
	private class ImageAsyncTask extends AsyncTask<Object, Void, Bitmap> {
		private ImageView image;
		@Override
		protected Bitmap doInBackground(Object... params) {
			String path = (String) params[0];
			String type = (String) params[1];
			image = (ImageView) params[2];
			Resources res = mContext.getResources();
			
			if(type.equals(res.getString(R.string.list_type_text))) {
				return null;
			} else if(type.equals(res.getString(R.string.list_type_photo))) {
				return ImageUtil.getPhotoSmallThumbnail(path);
			} else if(type.equals(res.getString(R.string.list_type_video))) {
				return ImageUtil.getVideoSmallThumbnail(path);
			} else {
				return null;
			}
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			if(null == result) {
				image.setVisibility(View.GONE);
				return;
			}
			image.setImageBitmap(result);
		}
		
	}
}
